package com.awenhui.demo.core.utils;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomListColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.awt.*;

/**
 * 验证码操作
 *
 * @author yuxf
 * @version 1.0
 * @date 2020/8/7 19:51
 */
public class CaptchaEngine extends ListImageCaptchaEngine {

    /**
     * 图片宽度
     */
    private static final int IMAGE_WIDTH = 80;
    /**
     * 图片高度
     */
    private static final int IMAGE_HEIGHT = 28;
    /**
     * 最小字体大小
     */
    private static final int MIN_FONT_SIZE = 12;
    /**
     * 最大字体大小
     */
    private static final int MAX_FONT_SIZE = 16;
    /**
     * 最小字符个数
     */
    private static final int MIN_WORD_LENGTH = 4;
    /**
     * 最大字符个数
     */
    private static final int MAX_WORD_LENGTH = 4;
    /**
     * 随机字符
     */
    private static final String CHAR_STRING = "23456789ABCDEFGHJKLMNPQRSTUVWXY";
    /**
     * 随机字体
     */
    private static final Font[] FONTS = new Font[]{new Font("nyala", Font.BOLD, MAX_FONT_SIZE), new Font("Arial", Font.BOLD, MAX_FONT_SIZE), new Font("nyala", Font.BOLD, MAX_FONT_SIZE), new Font("Bell", Font.BOLD, MAX_FONT_SIZE), new Font("Bell MT", Font.BOLD, MAX_FONT_SIZE), new Font("Credit", Font.BOLD, MAX_FONT_SIZE), new Font("valley", Font.BOLD, MAX_FONT_SIZE),
            new Font("Impact", Font.BOLD, MAX_FONT_SIZE)};
    /**
     * 随机颜色
     */
    private static final Color[] COLORS = new Color[]{new Color(218, 165, 32), new Color(139, 69, 19), new Color(128, 0, 0), new Color(25, 25, 112), new Color(0, 100, 0), new Color(47, 79, 79)};

    /**
     * 验证码图片生成
     */
    @Override
    protected void buildInitialFactories() {
        FontGenerator fontGenerator = new RandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE, FONTS);
        RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(COLORS);

        BackgroundGenerator backgroundGenerator =
                new UniColorBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT);
        TextPaster textPaster =
                new DecoratedRandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH,
                        randomListColorGenerator,
                        new TextDecorator[]{});
        addFactory(new GimpyFactory(new RandomWordGenerator(CHAR_STRING), new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster)));
    }
}
